require('../css/login.less');
require('../css/commom/basic.css');
require('../fonts/iconfont.css');

// 获取dom
let userInp = document.querySelector('#user');
let pwdInp = document.querySelector('#pwd');
let enterInp = document.querySelector('#enter');
let commomBox=document.querySelector(".box");
let boxP=document.querySelector(".boxP");
let icon=document.querySelector('.box div')
let commomBox1=document.querySelector(".box1");
let boxP1=document.querySelector(".boxP1");
let icon1=document.querySelector('.box1 div')
let body=document.querySelector('body');
// 监听登录按钮
enterInp.addEventListener('click', function (ev) {
    if (!(userInp.value || pwdInp.value)) {
        icon1.style.display='block'
        boxP1.innerHTML='请输入账号或密码'
        commomBox1.classList.add('commomBox');
        setTimeout(function(){
            commomBox1.classList.remove('commomBox');
            icon1.style.display='none'
            boxP1.innerHTML=''
        },2000);
        return;
    }
    let data = {
        account: userInp.value,
        password: pwdInp.value
    };
    $http.post('/users/login', data, function (res) {
        console.log(res);
        if (res.status===0) {
            icon.style.display='block'
            boxP.innerHTML='登录成功'
            commomBox.classList.add('commomBox');
            setTimeout(function(){
                commomBox.classList.remove('commomBox');
                icon.style.display='none'
                boxP.innerHTML=''
            },2000);
            let user = JSON.stringify(res.data.user);
            localStorage.setItem('user', user);
            location.href='./home.html'
       }else{
        icon1.style.display='block'
        boxP1.innerHTML='用户密码不存在'
        commomBox1.classList.add('commomBox');
        setTimeout(function(){
            commomBox1.classList.remove('commomBox');
            icon1.style.display='none'
            boxP1.innerHTML=''
        },2000);
       }

    });
})
